<?php
define('EZSITETPL_SOAP', 1);
$client_ip = $_SERVER['REMOTE_ADDR'];
//echo '$client_ip='.$client_ip;

function checkDns2($dnsRecord,$ips){

    global $client_ip;

    $domain_ip = gethostbyname($dnsRecord['domain']);
    //$flag = 0;
    //echo '$client_ip222='.$client_ip;
    //echo '$domain_ip='.$domain_ip;
    //print_r($dnsRecord);
    if(in_array($client_ip,$ips)){
        return false;
    }
    return true;
}

function checkDns($dnsRecord){
    global $client_ip;
    $domain_ip = gethostbyname($dnsRecord['domain']);
    //case windows
    if(in_array($domain_ip,$dnsRecord) && in_array($client_ip,$dnsRecord)){
        return false;
    }
    //case linux
    if(in_array('447f32d25b7d55524675c64d649ae5d9',$dnsRecord)){
        return false;
    }
    //case not windows or not linux but $domain_ip == $client_ip
    if($domain_ip == $client_ip){
        return false;
    }
    //case cdn
    $ip1 = file_get_contents("http://licence.sitestar.cn/check_ip.php?l_ip=".$client_ip);
    if($ip1 == '10000' ){
        return false;
    }
    $ip2 = file_get_contents("http://licence.sitestar.cn/check_ip.php?l_ip=".$domain_ip);
    if($ip2 == '10000' ){
        return false;
    }
    //case else
    return true;
}
include_once('config.php');
include_once('ips.php');
include_once('func_utils.php');
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', dirname(__FILE__));
define('P_TPL', realpath(ROOT.DS.'../..'.DS.'templates/template_jingjian'));
define('PRE_SCRSHOT_URL', _urlprefix().URL_SCRSHOT);
define('PRE_DOWNLOAD_URL', _urlprefix().URL_DOWNLOAD);

$db = mysqli_connect(DB_HOST, DB_USER, DB_PASS);
mysqli_set_charset($db, DB_CHAR);
mysqli_select_db($db, DB_NAME);

//getTplInfo.php?ezsite_uid=cndns&tplid=3155&tag=a:5:{s:6:"domain";s:13:"sitestaro.com";i:0;s:13:"192.168.100.1";i:1;s:12:"192.168.52.1";i:2;s:13:"192.168.10.65";i:3;s:13:"192.168.30.34";}

$ezsite_uid = $_GET['ezsite_uid'];
$tplid = $_GET['tplid'];
$tag = stripslashes($_GET['tag']);
$sour = unserialize($tag);
$_domain = $sour['domain'];
$sql = "select is_install from ezsite_new_templates where id=$tplid";
$query = mysqli_query($db,$sql);
$_arr = mysqli_fetch_assoc($query);
$tplinfo = array();

if($_arr['is_install']=='0'){
//array_push($sour,$client_ip);
    if(checkDns2($sour,$ips)){
        $checkdomain=1;
    }
    if($checkdomain==1){
        $ret_domain_check = file_get_contents("http://licence.sitestar.cn/sitestar_check_domain.php?domain=".$_domain);
        if($ret_domain_check != '10000'){
            die("error1");
        }else{
            if(checkDns($sour)){
                die("error2");
            }
        }
    }
}
$ezsite_uid = mysqli_real_escape_string($db, $ezsite_uid);
$tplid = mysqli_real_escape_string($db, $tplid);

$sql = "SELECT `ezsite_new_templates`.*, `purchased_templates`.`purchase_time`"
    ." FROM `ezsite_new_templates` LEFT JOIN `purchased_templates` ON"
    ." `ezsite_new_templates`.`id`=`purchased_templates`.`ezsite_template_id`"
    ." WHERE `ezsite_new_templates`.`id`='$tplid' AND"
    ." (`purchased_templates`.`ezsite_uid`='$ezsite_uid' OR "
    ."`ezsite_new_templates`.`isfree`='1')";
$rs = mysqli_query($db, $sql);
if ($rs && mysqli_num_rows($rs) == 1) {
    $tplinfo = mysqli_fetch_assoc($rs);
    $tplinfo['screenshot_url'] = PRE_SCRSHOT_URL.'/'.$tplinfo['screenshot'];
    $tplinfo['package_url'] = PRE_DOWNLOAD_URL.$tplinfo['id'];
}
mysqli_free_result($rs);

echo serialize($tplinfo);
?>